//
// Created by denglibin on 2021/8/13.
// 捕捉屏幕 wxScreenDC dc,屏幕设备上下文
#include <wx/app.h>
#include <wx/wx.h>
#include <iostream>
using namespace std;
/**
 * 捕捉屏幕到位图
 * @return
 */
wxBitmap GetScreenShot(){
    wxSize screenSize = wxGetDisplaySize();
    wxBitmap bitmap(screenSize.x, screenSize.y);
    wxScreenDC dc; //屏幕设备上下文
    wxMemoryDC memDc; //内存设备上下文
    memDc.SelectObject(bitmap);
    //将屏幕拷贝到memDC
    //memDc.Blit(0, 0, screenSize.x, screenSize.y, &dc, 0, 0);
    memDc.Blit(0, 0, screenSize.x/2, screenSize.y/2, &dc, 0, 0);
    return bitmap;
}
class ScreenFrame : public wxFrame
{
private:
    //声明事件表
DECLARE_EVENT_TABLE();
    wxBitmap screen;
    /**
     * 双缓冲绘图
     * @param event
     */
    void OnPaint_(wxPaintEvent & event){

        /* wxPaintDC 只能放在函数内*/
        wxPaintDC dc(this);
        //获取窗口大小
        //wxSize size = this->GetClientSize();
        /* 准备wxMemoryDC */
        wxMemoryDC memDC;
        wxRect win_rect = this->GetRect();
        wxBitmap bitmap(win_rect.GetSize());
        memDC.SelectObject(bitmap);



        //绘制位图
        memDC.DrawBitmap(this->screen, 0, 0, true);

        /* 绘图完毕 */
        /* 复制到wxPaintDC,Blit函数将内存设备上下文中的某一个区域拷贝到别的设备上下文上， */
        dc.Blit(wxPoint(0, 0), win_rect.GetSize(), &memDC, wxPoint(0, 0));
        memDC.SelectObject(wxNullBitmap);
    }

    /**
     * 初始化frame
     */
    void initFrame(){
        //默认情况下，当窗口大小改变时，只有那些需要重画的地方才会被更新，指定wx-
        //FULL REPAINT ON RESIZE窗口类型可以覆盖这种默认情况以使得整个窗口都被刷新
        wxFULL_REPAINT_ON_RESIZE;

        wxImage::AddHandler(new wxJPEGHandler);
        wxImage::AddHandler(new wxPNGHandler);
        //设置窗口位置
        wxPoint position;
        position.x = 300;
        position.y = 300;
        SetPosition(position);
        //设置背景色
        wxColour backGroundColor(0,100,0);
        this->SetBackgroundColour(backGroundColor);
        //设置宽高
        this->m_minWidth = 800;
        this->m_minHeight = 600;
    }

public:
    explicit ScreenFrame(wxString & title):wxFrame(nullptr, wxID_ANY, title)
    {
        this->initFrame();
        this->screen = GetScreenShot();
    }
    ~ScreenFrame() override{
        cout << "~ScreenFrame" << endl;
    };

};

//事件和函数绑定
wxBEGIN_EVENT_TABLE(ScreenFrame, wxFrame)
                EVT_PAINT(ScreenFrame::OnPaint_)
wxEND_EVENT_TABLE()

/**
 * app
 */
class ScreenApp:public wxApp
{
    bool OnInit()override
    {
        wxString title = wxT("屏幕捕捉");
        //  wxFrame * frame = new wxFrame(NULL, wxID_ANY, "第一个窗体");
        auto * frame = new ScreenFrame(title);
        frame->Show();
        cout << "OnInit"<< endl;
        return true;
    }
};


// 有了这一行就可以使用 OurApp& wxGetApp了()
DECLARE_APP(ScreenApp)
//告诉主应用程序是哪个类wxWidgets（启动）
//IMPLEMENT_APP(ScreenApp)

